<?php 
//@TODO переделай это в preprocess_node
function _tso_div_to_li($value) {
  if ($value) {
    $value = str_replace('<div class="field-item">', '<li>', $value);
    $value = str_replace('</div>', '</li>', $value);
    $value = '<ul>' . $value . '</ul>';

    return $value;
  } else return null;
}

//@TODO переделай это в preprocess_node
function _tso_div_to_comma($value) {
  $value = str_replace('<div class="field-item">', '', $value);
  $value = str_replace('</div>', ', ', $value);
  $value = _tso_comma_filter($value);

  return $value;
}

function _tso_comma_filter($value) {
  //брутальный способ избавления от лишней запятой в конце строки
  $value = explode(", ", $value);
  $value = array_filter($value);
  $value = implode(', ', $value);
  
  return $value;
}

function _tso_audience_sort($rows) {
  foreach ($rows as $count => $row) $audience[] = $row['title'];
  natsort($audience);
  foreach ($audience as $count => $row) $new_rows[$count] = $rows[$count];
  
  return $new_rows;
}

function blix_preprocess_node(&$vars, $hook) {
  global $user;
  setlocale(LC_TIME, 'ru_RU.UTF-8');

  $node = $vars['node'];
  switch ($node->type) {
    case  'audience':
      $field_equipment = $node->field_equipment;
      
      foreach ($field_equipment as $value) $equipment .= '<li>' . $value['value'] . '</li>';
      $field_equipment = '<ul>' . $equipment . '</ul>';
      
      $vars['field_description']        = $node->field_description[0]['view'];
      $vars['field_foto']               = $node->field_foto[0]['view'];
      $vars['field_quantity_of_seats']  = $node->field_quantity_of_seats[0]['view'];
      $vars['field_equipment']          = $field_equipment;
      break;
    case  'equipment':
      $field_presence_value = $node->field_presence;
      
      if ($field_presence_value[0]['value']) {
        foreach ($field_presence_value as $data) $field_presence .= '<li>' . $data['value'] . '</li>';
        $field_presence = '<lu>' . $field_presence . '</lu>';
      }
      
      $vars['field_description']        = $node->field_description[0]['view'];
      $vars['body']                     = $node->content['body']['#value'];
      $vars['field_description']        = $node->field_description[0]['view'];
      $vars['field_foto']               = $node->field_foto[0]['view'];
      $vars['field_presence']           = $field_presence;
      break;
    case 'member':
      $field_audience = '';
      $field_ats_phone = '';
      $field_work_phone = '';
    
      foreach ($node->field_ats_phone as $data) $field_ats_phone .= $data['view'] . ', ';
      foreach ($node->field_work_phone as $data) $field_work_phone .= $data['view'] . ', ';
      foreach ($node->field_audience as $data) $field_audience .= $data['view'] . ', ';
      foreach ($node->taxonomy as $data) $taxonomy = $data->name;
      
      $field_ats_phone = _tso_comma_filter($field_ats_phone);
      $field_work_phone = _tso_comma_filter($field_work_phone);
      $field_audience = _tso_comma_filter($field_audience);
      
      $vars['field_foto']       = $node->field_foto[0]['view'];
      $vars['field_position']   = $node->field_position[0]['view'];
      $vars['field_mail']       = $node->field_mail[0]['view'];
      $vars['field_ats_phone']  = $field_ats_phone;
      $vars['field_work_phone'] = $field_work_phone;
      $vars['field_audience']   = $field_audience;
      $vars['taxonomy']         = $taxonomy;      
      break;
      
    case 'table_event':
      $content = $node->content;

//      dvm($content['field_date']['field']['items'][0]);
//      print kpr($content['field_date']['field']['items'][0], TRUE);

      $date_from = $content['field_date']['field']['items'][0]['#item']['value'];
      $date_from = strtotime($date_from);

      $vars['body']     = $content['body']['#value'];
      $vars['time']     = strftime("%H:%M", $date_from);
      $vars['repeat']   = $content['field_repeat']['field']['items'][0]['#item']['value'];
      $vars['date']     = strftime("%d %B", $date_from);
      $vars['end_date'] = $end_date ? date('d.m.y', strtotime($end_date)) : null;
      break;
      
    case 'video':
      foreach ($node->taxonomy as $item) $taxonomy = $item->name;
      
      $vars['field_cover']        = $node->field_cover[0]['view'];
      $vars['field_id']           = $node->field_id[0]['view'];
      $vars['taxonomy']           = $taxonomy;
      $vars['field_part']         = $node->field_part[0]['view'];
      $vars['field_duration']     = $node->field_duration[0]['view'];
      $vars['field_release']      = $node->field_release[0]['view'];
      $vars['field_videosystem']  = $node->field_videosystem[0]['view'];
      $vars['field_link']         = $node->field_link[0]['view'];
      break;
      
    case 'news':
      if ($node->field_news_date[0]['value']){
        setlocale(LC_TIME, 'ru_RU.UTF-8');
        $submit_date = strftime('%e %b %Y', strtotime($node->field_news_date[0]['value']));
        
        $vars['submit_date'] = $submit_date;
      }
      
      $vars['teaser'] = $node->field_news_teaser[0]['value'];
      $vars['body'] = $node->content['body']['#value'];
      break;
      
    case 'greetings':
      $vars['teaser'] = $node->field_news_teaser[0]['value'];
      $vars['link']   = $node->field_link[0]['value'];
      $vars['body']   = $node->content['body']['#value'];
      break;
      
    case 'films_about_omgtu':
    case 'news_on_our_server':
    case 'academic_council_news':
      $vars['link'] = $node->field_link[0]['value'];
      $vars['body'] = $node->content['body']['#value'];
      break;
  }
}
